package crivia.ecp.carry.em;

import crivia.db.i.CMD;
import crivia.ecp.ECPExecuter;
import crivia.ecp.common.ECR;
import crivia.ecp.common.EcologyDB;
import crivia.ecp.i.DI;
import crivia.ecp.i.RequirdStringAble;
import crivia.ecp.p.implemented.CBB;

public abstract class Browser implements DI , RequirdStringAble {
	
	public static final String cbb(String pageKey){
		return "/"+CBB.PageKey+".c?p=" + pageKey;
	}
	
	public static final String BrowserURLSystemBase = "/systeminfo/BrowserMain.jsp?url=";
	public static final String BrowserURLSystemSingle(String type){
		return BrowserURLSystemBase
			+"/interface/CommonBrowser.jsp?type=browser."
			+type;
	} 
	public static final String BrowserURLSystemMultiple(String type){
		return BrowserURLSystemBase
			+"/interface/MultiCommonBrowser.jsp?type=browser."
			+type;
	} 
	
	public static final String URL_Hrm = BrowserURLSystemBase
			+"/hrm/resource/ResourceBrowser.jsp";
	public static final Browser s_Hrm(String key , String value , String requirdString){
		return staticBrowserBuilder(URL_Hrm
				, key, value , requirdString
				, "hrmResource", "id", "lastName")
			.setCompleteURL("/data.jsp");
	}
	public static final Browser s_Hrm(String key , String value){
		return s_Hrm(key, value, null);
	}
	public static final Browser s_Hrm(ECPExecuter page, String key){
		return s_Hrm(key, page.rp(key));
	}
	public static final Browser s_Hrm(ECPExecuter page, String key , String requirdString){
		return s_Hrm(key, page.rp(key), requirdString);
	}
	public static final String URL_HrmMuti = BrowserURLSystemBase
			+"/hrm/resource/MutiResourceBrowser.jsp";
	public static final Browser s_HrmMuti(String key , String value , String requirdString){
		return staticBrowserBuilder(URL_HrmMuti
				, key, value , requirdString
				, "hrmResource", "id", "lastName").setValueParam("resourceids")
				.setMulti(true);
	}
	public static final Browser s_HrmMuti(String key , String value){
		return s_HrmMuti(key, value, null);
	}
	public static final Browser s_HrmMuti(ECPExecuter page, String key){
		return s_HrmMuti(key, page.rp(key));
	}
	public static final Browser s_HrmMuti(ECPExecuter page, String key , String requirdString){
		return s_HrmMuti(key, page.rp(key), requirdString);
	}
	
	public static final String URL_SubCpy = BrowserURLSystemBase
			+"/hrm/company/SubcompanyBrowser.jsp";
	public static final Browser s_SubCpy(String key , String value , String requirdString){
		return staticBrowserBuilder(URL_SubCpy
				, key, value , requirdString
				, "hrmSubCompany", "id", "subCompanyName");
	}
	public static final Browser s_SubCpy(String key , String value){
		return s_SubCpy(key, value, null);
	}
	public static final Browser s_SubCpy(ECPExecuter page, String key){
		return s_SubCpy(key, page.rp(key));
	}
	public static final Browser s_SubCpy(ECPExecuter page, String key , String requirdString){
		return s_SubCpy(key, page.rp(key), requirdString);
	}
	public static final String URL_SubCpyMuti = BrowserURLSystemBase
			+"/hrm/company/MutiSubcompanyBrowser.jsp";
	public static final Browser s_SubCpyMuti(String key , String value , String requirdString){
		return staticBrowserBuilder(URL_SubCpyMuti
				, key, value , requirdString
				, "hrmSubCompany", "id", "subCompanyName")
				.setMulti(true);
	}
	public static final Browser s_SubCpyMuti(String key , String value){
		return s_SubCpyMuti(key, value, null);
	}
	public static final Browser s_SubCpyMuti(ECPExecuter page, String key){
		return s_SubCpyMuti(key, page.rp(key));
	}
	public static final Browser s_SubCpyMuti(ECPExecuter page, String key , String requirdString){
		return s_SubCpyMuti(key, page.rp(key), requirdString);
	}
	
	public static final String URL_Dept = BrowserURLSystemBase
			+"/hrm/company/DepartmentBrowser.jsp";
	public static final Browser s_Dept(String key , String value , String requirdString){
		return staticBrowserBuilder(URL_Dept
				, key, value , requirdString
				, "hrmDepartment", "id", "departmentName");
	}
	public static final Browser s_Dept(String key , String value){
		return s_Dept(key, value, null);
	}
	public static final Browser s_Dept(ECPExecuter page, String key){
		return s_Dept(key, page.rp(key));
	}
	public static final Browser s_Dept(ECPExecuter page, String key , String requirdString){
		return s_Dept(key, page.rp(key), requirdString);
	}
	@Deprecated
	public static final String URL_DeptMuti = BrowserURLSystemBase
			+"/hrm/company/MultiDepartmentBrowser.jsp";
	@Deprecated
	public static final Browser s_DeptMuti(String key , String value , String requirdString){
		return staticBrowserBuilder(URL_DeptMuti
				, key, value , requirdString
				, "hrmDepartment", "id", "departmentName")
				.setMulti(true);
	}
	@Deprecated
	public static final Browser s_DeptMuti(String key , String value){
		return s_DeptMuti(key, value, null);
	}
	@Deprecated
	public static final Browser s_DeptMuti(ECPExecuter page, String key){
		return s_DeptMuti(key, page.rp(key));
	}
	@Deprecated
	public static final Browser s_DeptMuti(ECPExecuter page, String key , String requirdString){
		return s_DeptMuti(key, page.rp(key), requirdString);
	}
	
	public static final String URL_DeptMutiTree = BrowserURLSystemBase
			+"/hrm/company/MutiDepartmentBrowser.jsp";
	public static final Browser s_DeptMutiTree(String key , String value , String requirdString){
		return staticBrowserBuilder(URL_DeptMutiTree
				, key, value , requirdString
				, "hrmDepartment", "id", "departmentName")
				.setMulti(true);
	}
	public static final Browser s_DeptMutiTree(String key , String value){
		return s_DeptMutiTree(key, value, null);
	}
	public static final Browser s_DeptMutiTree(ECPExecuter page, String key){
		return s_DeptMutiTree(key, page.rp(key));
	}
	public static final Browser s_DeptMutiTree(ECPExecuter page, String key , String requirdString){
		return s_DeptMutiTree(key, page.rp(key), requirdString);
	}
	
	public static final String URL_Request = BrowserURLSystemBase
			+"/workflow/request/RequestBrowser.jsp";
	public static final Browser s_Request(String key , String value , String requirdString){
		return staticBrowserBuilder(URL_Request
				, key, value , requirdString
				, "workflow_RequestBase", "requestID", "requestName");
	}
	public static final Browser s_Request(String key , String value){
		return s_Request(key, value, null);
	}
	public static final Browser s_Request(ECPExecuter page, String key){
		return s_Request(key, page.rp(key));
	}
	public static final Browser s_Request(ECPExecuter page, String key , String requirdString){
		return s_Request(key, page.rp(key), requirdString);
	}

	public static final String URL_Workflow = BrowserURLSystemBase
			+"/workflow/workflow/WorkflowBrowser.jsp";
	public static final Browser s_Workflow(String key , String value , String requirdString){
		return staticBrowserBuilder(URL_Workflow
				, key, value , requirdString
				, "workflow_Base", "id", "workflowName");
	}
	public static final Browser s_Workflow(String key , String value){
		return s_Workflow(key, value, null);
	}
	public static final Browser s_Workflow(ECPExecuter page, String key){
		return s_Workflow(key, page.rp(key));
	}
	public static final Browser s_Workflow(ECPExecuter page, String key , String requirdString){
		return s_Workflow(key, page.rp(key), requirdString);
	}
	
	public static final String URL_WorkflowMuti = BrowserURLSystemBase
			+"/workflow/workflow/MutiWorkflowBrowser.jsp";
	public static final Browser s_WorkflowMuti(String key , String value , String requirdString){
		return staticBrowserBuilder(URL_WorkflowMuti
				, key, value , requirdString
				, "workflow_Base", "id", "workflowName")
			.setMulti(true);
	}
	public static final Browser s_WorkflowMuti(String key , String value){
		return s_WorkflowMuti(key, value, null);
	}
	public static final Browser s_WorkflowMuti(ECPExecuter page, String key){
		return s_WorkflowMuti(key, page.rp(key));
	}
	public static final Browser s_WorkflowMuti(ECPExecuter page, String key , String requirdString){
		return s_WorkflowMuti(key, page.rp(key), requirdString);
	}
	
	protected Browser(String url, String key, String value, String requirdString) {
		this.url = url;
		this.key = key;
		this.value = ECR.ns(value);
		this.requirdString = requirdString;
		this.requird = ECR.hasCondition(requirdString);
	}
	
	private static final Browser staticBrowserBuilder(String url
			, String key , String value , String requirdString
			, String table , String id , String name){ 
		return create(url, key, value, requirdString)
				.relation(table, id, name);
	}

//	private static final String BrowserButtomImage_E7 = "/wui/theme/ecology7/skins/red/general/browser.png";
//	private static final String BrowserButtomImage_E8 = "/wui/theme/ecology7/skins/red/general/browser_wev8.png";

	public static Browser create(String url){
		return create(url, "");
	}
	
	public static Browser create(String url , String key) {
		return create(url, key, "", null);
	}
	
	public static Browser create(String url , String key, String value) {
		return create(url, key, value, null);
	}
	
	public static Browser create(String url , String key, String value, String requirdString) {
		if ("8".equals(ECPExecuter.ep(ECPExecuter.EPK_Version))){
			return new Browser8(url , key , value , requirdString);
		} else {
			return new Browser7(url , key , value , requirdString);
		}
	}
	
	public static Browser create(ECPExecuter page, String url , String key) {
		return create(url, key, ECR.ns(page.rp(key)), null);
	}
	
	public static Browser create(ECPExecuter page, String url , String key, String requirdString) {
		return create(url, key, ECR.ns(page.rp(key)), null);
	}
	
	protected String key;
	public Browser setKey(String key) {
		this.key = key;
		return this;
	}
	public String getKey() {
		return ECR.ns(key);
	}
	
	protected String scriptKey;
	public String getScriptKey() {
		if (!ECR.hasCondition(scriptKey)){
			scriptKey = key;
		}
		return scriptKey;
	}
	public Browser setScriptKey(String scriptKey) {
		this.scriptKey = scriptKey;
		return this;
	}
	
	protected String value;
	public Browser setValue(String value) {
		this.value = ECR.ns(value);
		return this;
	}
	public String getValue() {
		return value;
	}
	
	protected String valueParam;
	public String getValueParam() {
		return valueParam;
	}
	public Browser setValueParam(String valueParam) {
		this.valueParam = valueParam;
		return this;
	}
	
	protected String defaultValue;
	public String getDefaultValue() {
		return defaultValue;
	}
	public Browser setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
		return this;
	}
	
	protected boolean requird = false;
	public boolean isRequird() {
		return requird;
	}
	public Browser setRequird(boolean requird) {
		this.requird = requird;
		return this;
	}
	
	protected String requirdString;
	public String getRequirdString() {
		return requirdString;
	}
	public Browser setRequirdString(String requirdString) {
		this.requirdString = requirdString;
		requird = ECR.hasCondition(requirdString);
		return this;
	}
	
	protected int requirdStringLevel;
	public int getRequirdStringLevel() {
		return requirdStringLevel;
	}
	public Browser setRequirdStringLevel(int requirdStringLevel) {
		this.requirdStringLevel = requirdStringLevel;
		return this;
	}

	protected String requirdStringScript;
	public String getRequirdStringScript() {
		return requirdStringScript;
	}
	public Browser setRequirdStringScript(String requirdStringScript) {
		this.requirdStringScript = requirdStringScript;
		return this;
	}
	
	protected String url;
	public String getUrl() {
		return url;
	}
	public Browser setUrl(String url) {
		this.url = url;
		return this;
	}
	
	protected String relationTable;
	public String relationTable() {
		return relationTable;
	}
	public Browser relationTable(String table) {
		relationTable = table;
		return this;
	}
	
	protected String relationID;
	public String relationID() {
		return relationID;
	}
	public Browser relationID(String id) {
		relationID = id;
		return this;
	}
	
	protected String relationName;
	public String relationName() {
		return relationName;
	}
	public Browser relationName(String name) {
		this.relationName = name;
		return this;
	}
	
	public Browser relation(String table , String id , String name){
		relationTable(table);
		relationID(id);
		relationName(name);
		return this;
	}
	public Browser relation(Enum<?> table , Enum<?> id , Enum<?> name){
		return relation(""+table, ""+id, ""+name);
	}
	public Browser relation(String table , Enum<?> id , Enum<?> name){
		return relation(table, ""+id, ""+name);
	}
	public Browser relation(String table , String id , Enum<?> name){
		return relation(table, id, ""+name);
	}
	public Browser relation(String table , Enum<?> id , String name){
		return relation(table, ""+id, name);
	}
	
	public boolean relationReady(){
		return true
			&& ECR.hasCondition(relationTable)
			&& ECR.hasCondition(relationID)
			&& ECR.hasCondition(relationName);
	}
	
	protected String fixText;
	public String getFixText() {
		return fixText;
	}
	public Browser setFixText(String fixText) {
		this.fixText = fixText;
		return this;
	}
	
	private boolean hasWidth = false;
	protected String width = "165px";
	public String getWidth() {
		return width;
	}
	public Browser setWidth(String width) {
		hasWidth = true;
		this.width = width;
		return this;
	}
	public boolean isHasWidth() {
		return hasWidth;
	}
	
	protected String completeURL;
	public String getCompleteURL() {
		return completeURL;
	}
	public Browser setCompleteURL(String completeURL) {
		this.completeURL = completeURL;
		return this;
	}
	
	protected boolean Multi = false;
	public boolean isMulti() {
		return Multi;
	}
	public Browser setMulti(boolean multi) {
		Multi = multi;
		return this;
	}
	
	protected CMD relationCMD = EcologyDB.db();
	public CMD getRelationCMD() {
		return relationCMD;
	}
	public Browser setRelationCMD(CMD relationCMD) {
		this.relationCMD = relationCMD;
		return this;
	}
	
	@Override
	public String getText() {
		return ECR.hasCondition(value) && relationReady()
				? crivia.db.common.SQL.relation(ECR.ns(value)
						, relationTable, relationName, relationID, relationCMD) : "";
	}
	
	@Override
	public abstract String toString();
	
}
